package com.gmako.controller;

import java.net.URLDecoder;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.gmako.entity.Adress;
import com.gmako.service.AdressManageService;
import com.gmako.utils.Constants;
import com.gmako.utils.Pager;
import com.gmako.utils.ResponseUtils;

@Controller
@RequestMapping("/adress.do")
public class AdressManageController {
	@Autowired(required = false)
	private AdressManageService adressManageService;

	/**
	 * 显示地址分类界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=viewAdress")
	public ModelAndView viewClothingType(HttpServletRequest request,
			HttpServletResponse response) {
		List list = adressManageService.findByProperty("Adress","pid", 0);
		request.setAttribute("list", list);
		int pageNo=this.getPageNo(request);
		int pageSize = 10;
		Pager pager = adressManageService.findTypes(pageSize, pageNo);
		request.setAttribute("pager", pager);
		return new ModelAndView("/admin/adress");
	}
	
	/**
	 * 添加地址分类
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=addAdress")
	public ModelAndView addClothingType(HttpServletRequest request,
			HttpServletResponse response,Adress adress) {
		String name = request.getParameter("type");
		int cid = Integer.parseInt(request.getParameter("cid"));
		adress.setPid(cid);
		adress.setAdressName(name);
		adressManageService.save(adress);
		return new ModelAndView("redirect:/adress.do?method=viewAdress");
	}
	
	/**
	 * 判断删除项是否含有子分类
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=deleteClothingType")
	public ModelAndView deleteClothingType(HttpServletRequest request,
			HttpServletResponse response){
		int id = Integer.parseInt(request.getParameter("id"));
		List type=adressManageService.findByProperty("Adress", "pid", id);
		if(type.size()!=0)
		{
			ResponseUtils.renderText(response, "该分类含有子分类，是否继续删除?");
		}
		else
		{
			ResponseUtils.renderText(response, "1");
		}
		return null;
	}
	
	/**
	 * 删除某条地址
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=deleteOneClothingType")
	public ModelAndView deleteOneClothingType(HttpServletRequest request,
			HttpServletResponse response){
		int id = Integer.parseInt(request.getParameter("id"));
		adressManageService.delete(Adress.class, id);
		return new ModelAndView("redirect:/adress.do?method=viewAdress");
	}
	
	/**
	 * 删除某地址及城市
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=deleteAllClothingType")
	public ModelAndView deleteAllClothingType(HttpServletRequest request,
			HttpServletResponse response){
		int id = Integer.parseInt(request.getParameter("id"));
		List type=adressManageService.findByProperty("Adress", "pid", id);
		for(int i=0;i<type.size();i++)
		{
			Adress adress =(Adress)type.get(i);
			int typeid=adress.getId();
			adressManageService.delete(Adress.class, typeid);
		}
		adressManageService.delete(Adress.class, id);
		return new ModelAndView("redirect:/adress.do?method=viewAdress");
	}
	
	/**
	 * 跳转到修改地址界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=toUpdateClothingType")
	public ModelAndView toUpdateClothingType(@RequestParam("id") String id,HttpServletRequest request,
			HttpServletResponse response) {
		List type=adressManageService.findByProperty("Adress", "id", Integer.parseInt(id));
		request.setAttribute("type", type.get(0));
		List flist=adressManageService.findByProperty("Adress", "pid",0);
		request.setAttribute("flist", flist);
		String error=request.getParameter("error");
		if(error!=null)
		{
		request.setAttribute("error", "该分类已经含有有子分类，不能选择父分类!");
		}
		return new ModelAndView("/admin/updateType");
	}
	
	/**
	 * 查询地址是否重名
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(params = "method=findTypeIs")
	public void findTypeIs(HttpServletRequest request,
			HttpServletResponse response,Adress adress) throws Exception {
		String name = URLDecoder.decode(request.getParameter("type").toString(), "utf-8");
		int tag = adressManageService.findTypeIs(name);
		if(tag==1){
			ResponseUtils.renderText(response, "1");
		}
		else{
			ResponseUtils.renderText(response, "0");
		}
	}
	
	/**
	 * 修改地址界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=updateClothingType")
	public ModelAndView updateClothingType(HttpServletRequest request,
			HttpServletResponse response,Adress adress) {
		int id =Integer.parseInt(request.getParameter("id"));
		int cid = Integer.parseInt(request.getParameter("cid"));
		String name=request.getParameter("type");
		if(cid!=0)
		{
			List type=adressManageService.findByProperty("Adress", "pid", id);
			if(type.size()==0)
			{
				adress.setId(id);
				adress.setPid(cid);
				adress.setAdressName(name);
				adressManageService.update(adress);
				return new ModelAndView("redirect:/adress.do?method=viewAdress");
			}
			else
			{
				//request.setAttribute("error", "该分类下有子分类，不能选择父分类！");
				return new ModelAndView("redirect:/adress.do?method=toUpdateClothingType&id="+id+"&error="+1);
			}
		}
		else
		{
			adress.setId(id);
			adress.setPid(cid);
			adress.setAdressName(name);
			adressManageService.update(adress);
			return new ModelAndView("redirect:/adress.do?method=viewAdress");
		}
	}
	
	/**
	 * 获取当前页码
	 * 
	 * @author lixuewei
	 * @param request
	 * @return int
	 */
	public int getPageNo(HttpServletRequest request) {
		int pageNo = 0;
		if (request.getParameter("pageNo") != null
				&& !"".equals(request.getParameter("pageNo"))) {
			pageNo = Integer.parseInt(request.getParameter("pageNo"));

		} else {
			pageNo = Constants.pageNo;
		}
		return pageNo;
	}
}
